using UnityEngine;
using System.Collections;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;


public class BattleNetHandler : Singleton< BattleNetHandler > 
{
	
	void Awake ()
	{
		if ( mInstance == null )
		{
			mInstance = this;
			initHandler();
		}
		
	}
	
	public void initHandler()
	{
		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_BATTLE , recvBattle , typeof( GameNetMessage.RECV_MSG_BATTLE ) );

		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_BATTLE_RESULT , recvBattleResult , typeof( GameNetMessage.RECV_MSG_BATTLE_RESULT ) );



	}


	public void recvBattle( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.RECV_MSG_BATTLE msg = ( GameNetMessage.RECV_MSG_BATTLE )head;

		GamePlayerData.instance.BattleMapID = SAMapManager.instance.getBattleMap();

		GamePlayerData.instance.Pos = msg.Pos;

		SAMapManager.instance.mainPlayer.stopMoving();
		SAMapManager.instance.updateMainPlayer( SAMapManager.instance.mainPlayer );

		int index = 0;
		byte[] buff = GameSocketManager.instance.getIbuffer( out index );
		SABattleCreatureData[] data = GameDefine.bytesToStructArray< SABattleCreatureData >( buff , index + 17 , typeof( SABattleCreatureData ) , msg.PetNum + msg.EnemyNum + msg.EnemyPetNum );
		
		GameBattleData.instance.PVP = msg.PVP;
		GameBattleData.instance.Side = msg.Side;
		GameBattleData.instance.PetNum = msg.PetNum;
		GameBattleData.instance.EnemyNum = msg.EnemyNum;
		GameBattleData.instance.EnemyPetNum = msg.EnemyPetNum;
		
		GameBattleData.instance.Data = data;

		LoadingUIHandler.instance.loadScene( 2 );
	}


	public unsafe void recvBattleResult( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.RECV_MSG_BATTLE_RESULT msg = ( GameNetMessage.RECV_MSG_BATTLE_RESULT )head;

		int len = msg.head.size - 7;
		byte[] data = new byte[ len ];

		for ( int i = 0; i < len ; i++ )
		{
			data[ i ] = msg.Data[ i ];
		}

		SABattleResultManager.instance.playResult( msg.Turn , msg.Result , msg.Num , data );
	}



}
